package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.article.dtos.SensitivePageDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.AdSensitive;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.SensitiveMapper;
import com.heima.wemedia.service.SensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;

@Service
@Slf4j
@Transactional
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, WmSensitive> implements SensitiveService {


    /**
     * 查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findAll(SensitivePageDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        dto.checkParam();//分页检查
        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (dto.getName()!=null){
            lambdaQueryWrapper.like(WmSensitive::getSensitives,dto.getName()); //模糊
        }
        lambdaQueryWrapper.orderByDesc(WmSensitive::getCreatedTime);//倒序
        page = page(page, lambdaQueryWrapper);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());

        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 新增
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult newIncreased(AdSensitive adSensitive) {

    if (adSensitive==null){
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
        WmSensitive wmSensitive=new WmSensitive();
    if (wmSensitive.getSensitives().equals(adSensitive.getSensitives())){
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
    }else if (wmSensitive.getSensitives()!=adSensitive.getSensitives()){
        adSensitive.setSensitives(wmSensitive.getSensitives());
        }

        adSensitive.setCreatedTime(String.valueOf(System.currentTimeMillis()));
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 更新
     * @param adSensitive
     */
    @Override
    public void updateSensitive(AdSensitive adSensitive) {
        if (adSensitive==null){
           ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        update(Wrappers.<WmSensitive>lambdaUpdate().eq(WmSensitive::getId, adSensitive.getId()).set(WmSensitive::getSensitives, adSensitive.getSensitives()).set(WmSensitive::getCreatedTime, System.currentTimeMillis()));

    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult delSensitive(@RequestBody Integer id) {
        if (id==null){
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive byId = getById(id);
        if (byId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
